<template>
    <!-- 人员管理-工种字典 -->
    <div class="empWorktype">
        <div class="bottom">
            <div class="down">
                <el-button type="primary" @click="updateRow(0)"  size="small" plain :disabled="!btnList.includes('emp/workType/add')">添加工种</el-button>
            </div>
            <div class="table">
                <el-table :data="list" style="width: 100%" border :height="tableHeight" :stripe='true'>
                    <el-table-column fixed prop="workTypeName" label="名称" style="width: 10%"></el-table-column>
                    <el-table-column  label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button @click.native.prevent="updateRow(1,scope.$index, list)" type="text" size="small" :disabled="!btnList.includes('emp/workType/edit')">编辑</el-button>
                            <el-button  @click.native.prevent="remove(scope.$index, list)" type="text" size="small" style="color:red" :disabled="!btnList.includes('emp/workType/delete')">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="clearfix" v-show="list.length>0">
                <div class="float-right">
                    <Page v-if="totalCount" style="margin: 20px 0;text-align: center;" :current.sync="current" :total="totalCount" :page-size="pageSize" show-elevator show-total show-sizer @on-change="pageChange" @on-page-size-change="pageSizeChange"></Page>
                </div>
            </div>
        </div>

        <Modal v-model="updateModal" :title="isAdd ? '添加工种' : '编辑工种' " width="500px" :mask-closable="false">
            <div class="item">
                <span class="tip">*</span>
                <span>名称:</span>
                <Input  v-model="workTypeName"  placeholder="请输入工种名称" style="width:350px;" clearable />
            </div>
            <div slot="footer">
                <el-button @click="cancel(1)" type="info" size="small" plain>取消</el-button>
                <el-button @click="submitAll" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>

        <Modal v-model="deleteModal" title="删除" width="360px;" :mask-closable="false">
            <div style="margin:10px">确定要删除这条数据吗？</div>
            <div slot="footer">
                <el-button @click="cancel(2)" type="info" size="small" plain>取消</el-button>
                <el-button @click="detele" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>
    </div>
</template>

<script>

import {
    getEmpWorkTypeList,
    getEmpWorkTypeAdd,
    getEmpWorkTypeEdit,
    getEmpWorkTypeDel,
} from "@/api/api.js";
export default {
    data() {
        return {
            current: 1,
            pageSize: 10,
            pageNum: 1,
            totalCount: "",
            deptId: "",
            list:[],
            deleteModal:false,
            updateModal:false,
            workTypeId:"",
            workTypeName:"",
            isAdd:false,
            tableHeight:window.innerHeight - 325 ,
            btnList:[],
        };
    },
    computed: {
        porjectObject() {
            return this.$store.state.porjectObject;
        }
    },
    watch: {
        porjectObject(val, oldVal) {
            if (val.deptId != oldVal.deptId) {
                this.deptId = val.deptId;
                this.deptName = val.name;
                this.pageNum = 1;
                this.current = 1;
                this.keyword ="";
                this.value =[];
                this.getAllList();
            }
        }
    },
    methods: {
        getAllList() {
            let params = {
                pageSize: this.pageSize,
                pageNum: this.pageNum,
                deptId: this.deptId,
            };
            getEmpWorkTypeList(params)
                .then(res => {
                    if (res.code == 200) {
                        this.list = res.data.list;
                        this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        getTime(value){
            this.startDate =value[0];
            this.endDate =value[1]
        },
        pageChange(index) {
            this.pageNum = index;
            this.getAllList();
        },
        pageSizeChange(index){
            this.pageSize = index;
            this.getAllList();
        },
        
        updateRow(type,index,rows) {
            this.updateModal = true;
            if(type == 0) {
               this.workTypeName = "";
               this.isAdd = true;
            } else if (type== 1) {
               this.isAdd =false;
               this.workTypeName = rows[index].workTypeName;
               this.workTypeId = rows[index].workTypeId
            }
        },
        submitAll() {
            if (this.workTypeName == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请填写工种名称"
                });
                return;
            }
            let flag = this.list.some( v => v.workTypeName == this.workTypeName)
            if (flag) {
                this.$Message.warning('添加工种重复，请重新编辑工种名称');
                return;
            }
            if(this.isAdd){
                let params = {
                    deptId:this.deptId,
                    workTypeName:this.workTypeName,
                };
                getEmpWorkTypeAdd(params)
                    .then(res => {
                        if (res.code == 200) {
                            this.$Message.success("操作成功");
                            this.updateModal = false;
                            this.getAllList();
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            } else {
                let params = {
                    deptId:this.deptId,
                    workTypeName:this.workTypeName,
                    workTypeId:this.workTypeId,
                };
                getEmpWorkTypeEdit(params)
                    .then(res => {
                        if (res.code == 200) {
                            this.$Message.success("操作成功");
                            this.updateModal = false;
                            this.getAllList();
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    }); 
            }
        },
        remove(index, rows) {
            this.workTypeId = rows[index].workTypeId;
            this.deleteModal = true;
        },
        detele() {
            getEmpWorkTypeDel(this.workTypeId)
                .then(res => {
                    if (res.code == 200) {
                        this.$Message.success("操作成功");
                        this.getAllList();
                        this.deleteModal = false;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },

        cancel(type) {
            if (type == 1) {
                this.updateModal = false;
            } else if (type == 2) {
                this.deleteModal = false;
            }
        }
    },
    mounted() {
        this.deptId = this.$store.state.porjectObject.deptId;
        this.deptName = this.$store.state.porjectObject.name;
        this.getAllList();
        this.btnList = JSON.parse(localStorage.getItem("arr"));
    }
};
</script>


<style scoped lang="less">
@import "./../../assets/style/themeBase.less";
.empWorktype {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: #f5f5f5;
    .bottom{
        width: 100%;
        height: 100%;
        padding: 20px;
        box-sizing: border-box;
        background: #fff;
        border-radius: 10px;
        .down {
            margin-bottom: 20px;
        }
    }
}
.item {
    width: 400px;
    display: flex;
    margin: 0 auto;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    position: relative;
    margin-bottom: 20px;
    padding-left: 16px;
    box-sizing: border-box;
    .tip {
        position: absolute;
        top:8px;
        left: 0;
        color: red;
        font-size: 16px;
    }
}
</style>
